package cn.edu.jju.psytest.model.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.jju.psytest.model.constant.UserConstant;
import cn.edu.jju.psytest.model.entity.Question;
import cn.edu.jju.psytest.model.entity.Quiz;
import cn.edu.jju.psytest.model.entity.QuizRecord;
import cn.edu.jju.psytest.model.entity.Student;
import cn.edu.jju.psytest.model.entity.Teacher;
import cn.edu.jju.psytest.model.entity.User;
import cn.edu.jju.psytest.model.vo.QuizRecordVo;
import cn.edu.jju.psytest.model.vo.QuizVo;
import cn.edu.jju.psytest.model.vo.QuizVo._QuestionVo;
import cn.edu.jju.psytest.model.vo.QuizzesVo;
import cn.edu.jju.psytest.model.vo.QuizzesVo._QuizVo;
import cn.edu.jju.psytest.model.vo.StudentVo;
import cn.edu.jju.psytest.model.vo.TeacherVo;

public class EntityVoConverter {

	/**
	 * quizzes -> QuizzesVo
	 */
	public static QuizzesVo getQuizzesVo(List<Quiz> quizzes) {
		QuizzesVo quizzesVo = new QuizzesVo();
		List<_QuizVo> quizVos = new ArrayList<_QuizVo>();
		for (Quiz quiz : quizzes) {
			_QuizVo quizVo = new _QuizVo();
			quizVo.setQuizName(quiz.getName());
			quizVo.setLinkUrl("quizzes/" + quiz.getQuizId());
			quizVos.add(quizVo);
		}
		quizzesVo.setQuizVos(quizVos);
		return quizzesVo;
	}

	/**
	 * quiz -> QuizVo
	 */
	public static QuizVo getQuizVo(Quiz quiz) {
		QuizVo quizVo = new QuizVo();
		quizVo.setQuizId(quiz.getQuizId());
		quizVo.setName(quiz.getName());
		quizVo.setDescription(quiz.getDescription());
		quizVo.setCreatedDate(quiz.getCreatedDate());
		quizVo.setQuestionVos(getQuestionVoList(quiz.getQuestions()));
		return quizVo;
	}

	private static List<_QuestionVo> getQuestionVoList(List<Question> questions) {
		List<_QuestionVo> questionVoList = new ArrayList<_QuestionVo>();
		for (Question question : questions) {
			_QuestionVo questionVo = new _QuestionVo();
			questionVo.setQuestionId(question.getQuestionId());
			questionVo.setContent(question.getContent());
			questionVo.setOptions(getOptionList(question.getOptions()));
			questionVoList.add(questionVo);
		}
		return questionVoList;
	}

	private static List<String> getOptionList(String optionsS) {
		List<String> optionList = new ArrayList<String>();
		String[] options = optionsS.split(":");
		for (String option : options) {
			optionList.add(option);
		}
		return optionList;
	}

	/**
	 * QuizRecordVo -> QuizRecord
	 */
	public static QuizRecord getQuizRecord(QuizRecordVo quizRecordVo,
			String studentId) {
		QuizRecord quizRecord = new QuizRecord();
		quizRecord.setStudentId(studentId);
		quizRecord.setQuizId(quizRecordVo.getQuizId());
		quizRecord.setAnswers(quizRecordVo.getAnswers());
		quizRecord.setCreatedDate(new Date());
		return quizRecord;
	}

	/**
	 * studentVo -> UserEntity
	 */
	public static void studentVo2UserEntity(StudentVo studentVo, User user,
			Student student) {
		user.setUserId(studentVo.getStudentId());
		user.setPassword(studentVo.getPassword());
		user.setUserType(UserConstant.STUDENT);

		student.setStudentId(studentVo.getStudentId());
		student.setName(studentVo.getName());
		student.setGender(studentVo.getGender());
		student.setAge(studentVo.getAge());
		student.setSecondaryCollege(studentVo.getSecondaryCollege());
		student.setMajor(studentVo.getMajor());
		student.setClazz(studentVo.getClazz());
		student.setBornPlace(studentVo.getBornPlace());
		student.setHealthStatus(studentVo.getHealthStatus());
		student.setRaiseMethod(studentVo.getRaiseMethod());
		student.setRelativesHealth(studentVo.getRelativesHealth());
		student.setWillConsult(studentVo.getWillConsult());
		student.setHavePessiThoughts(studentVo.getHavePessiThoughts());
	}

	/**
	 * teacherVo -> UserEntity
	 */
	public static void teacherVo2UserEntity(TeacherVo teacherVo, User user,
			Teacher teacher) {
		user.setUserId(teacherVo.getTeacherId());
		user.setPassword(teacherVo.getPassword());
		user.setUserType(UserConstant.TEACHER);

		teacher.setTeacherId(teacherVo.getTeacherId());
		teacher.setPrivilege(teacherVo.getPrivilege());
	}
}
